<?php

namespace Modules\User\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\User\Entities\User;
use Modules\User\Entities\Address;

use Auth;
use Validator;
use ThrowException;

class AddressController extends Controller
{
    // 地址列表
    public function index(Request $request)
    {
        $user = Auth::requireLoginUser();
        $addresses = Address::getList([
            'builder' => $user->addresses(),
            'only' => [
                'id',
                'uuid',
                'province',
                'city',
                'district',
                'address',
                'consignee',
                'phone',
                'is_default',
                'tag',
            ]
        ]);

        return [
            'addresses' => $addresses->getArray()
        ];
    }

    // 新建地址
    public function store(Request $req)
    {
        $rule = [
            'province' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $info = $req->all();

        // \Log::error($info);

        $user = Auth::requireLoginUser();

        $info['user_id'] = $user->id;

        if (@$info['action']) {
            unset($info['action']);
        }

        $address = Address::create($info);

        if ($req->is_default)
            $address->setDefault();

        return [
            'id' => $address->id,
            'uuid' => $address->uuid
        ];
    }

    // 单个地址详情
    public function show(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();
        $item = Address::getEntity([
            'uuid' => $uuid,
            'user_id' => $user->id
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    } 

    // 更新地址
    public function update(Request $req, $addressUuid)
    {
        $rule = [
            'type' => ['in:update,set_default'],
            // 'attributes' => ['required_if:type,update', 'array']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();
        $address = Address::getEntity([
            'uuid' => $addressUuid,
            'user_id' => $user->id
        ]);

        if ($req->type === 'update') {
            $info = $req->input('attributes');
            if (is_string($info)) {
                $info = json_decode($info);
            }
            $address->update($info);
            if ($address->is_default)
                $address->setDefault();
        }
        elseif ($req->type === 'set_default')
            $address->setDefault();

        return [];
    }

    // 删除地址
    public function destroy(Request $req, $addressUuid)
    {
        $user = Auth::requireLoginUser();
        $address = $user->addresses()->where('uuid', $addressUuid)->first();
        $address || ThrowException::Conflict('地址不存在');
        $address->delete();

        return [];
    }
}
